package sourceagile.server.subversionFileRepository.edit;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.io.SVNRepository;

import sourceagile.server.subversionFileRepository.SubversionRepositoryConnection;

public class SaveFeatureFile {

	public static void save(String repositoryURL, String username,
			String password, String parentFolderName, String newSubFolderName,
			String fileName, String featureName, String featureDescription,
			String priority, String estimate, String status) {

		SVNRepository repository;
		try {

			repository = SubversionRepositoryConnection.connect(repositoryURL,
					username, password);

			AddFile.save(
					repository,
					parentFolderName,
					newSubFolderName,
					fileName,
					getFileContent(featureName, featureDescription, priority,
							estimate, status));

		} catch (SVNException e) {

			e.printStackTrace();

		}
	}

	private static String getFileContent(String featureName,
			String featureDescription, String priority, String estimate,
			String status) {

		String nameLine = "Feature: " + featureName + "\n";

		String descriptionLine = "";
		if (featureDescription != null) {

			descriptionLine = "# " + featureDescription + "\n";
		}

		String priorityLine = "";
		if (priority != null) {

			priorityLine = "# @Priority " + priority + "\n";
		}

		String estimateLine = "";
		if (estimate != null) {

			estimateLine = "# @Estimate " + estimate + "\n";
		}

		String statusLine = "";
		if (status != null) {

			statusLine = "# @Status " + status + "\n";
		}

		String fileContent = nameLine + descriptionLine + "\n" + priorityLine
				+ estimateLine + statusLine;

		return fileContent;
	}
}
